package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class RandomDice extends Sprite
	{
		private const MINIMUM_DICE_VALUE:uint = 1;
		private const MAXIMUM_DICE_VALUE:uint = 6;
		
		private const DICE_COLORS_ARRAY:Array = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF];
		
		private var _diceNumber:uint;
		private var _diceText:TextField;
		
		private var timer:Timer;
		
		public function RandomDice()
		{
			super();
			
			timer = new Timer(100, 0);
			timer.addEventListener(TimerEvent.TIMER, startFlashing);
			
			_diceText = new TextField();
			_diceText.y = -8;
			_diceText.width = 50;
			_diceText.selectable = false;
			
			var textFormat:TextFormat = new TextFormat();
			textFormat.align = "center";
			textFormat.size = 50;
			textFormat.font = "Tahoma";
			_diceText.setTextFormat(textFormat);
			_diceText.defaultTextFormat = textFormat;
			
			roll();
			
			this.addChild(_diceText);
		}
		
		private function startFlashing(e:Event):void
		{
			this.visible = !this.visible;
		}
		
		public function get diceNumber():uint
		{
			return _diceNumber;
		}
		
		public function stopFlash():void
		{
			timer.stop();
			this.visible = true;
		}
		
		public function startFlash():void
		{
			timer.start();
		}
		
		public function roll():void
		{
			_diceNumber = Util.getRandomBetweenInterval(MINIMUM_DICE_VALUE, MAXIMUM_DICE_VALUE);
			
			this.graphics.clear();
			this.graphics.beginFill(DICE_COLORS_ARRAY[_diceNumber-1]);
			this.graphics.drawRect(0, 0, 50, 50);
			this.graphics.endFill();
			
			_diceText.text = String(diceNumber);
		}
		
		public function setValue(value:String):void
		{
			this.graphics.clear();
			this.graphics.beginFill(0xC0C0C0);
			this.graphics.drawRect(0, 0, 50, 50);
			this.graphics.endFill();
			
			_diceText.text = String(value);
		}
		
		
		public function diceValue():String
		{
			return _diceText.text;
		}
	}
}